package com.fhx.core.utils

import android.content.Context
import android.os.Handler
import android.widget.Toast
import androidx.annotation.StringRes
import com.fhx.core.AppContext

object ToastUtils {
    var mToast: Toast? = null
//    val mHandler: Handler = Handler()
//    val mRunnable: Runnable = Runnable {
//        if (mToast != null) {
//            mToast!!.cancel()
//            mToast = null
//        }
//    }

    fun show(@StringRes res: Int) {
        show(AppContext.instance, AppContext.instance.getString(res))
    }

    fun show(msg: CharSequence) {
        show(AppContext.instance, msg)
    }

//    fun show(context: Context, msg: CharSequence) {
//        mHandler.removeCallbacks(mRunnable)
//        if (mToast != null) {
//            mToast!!.setText(msg)
//        } else {
//            mToast = Toast.makeText(context, msg, Toast.LENGTH_SHORT)
//            mToast!!.setText(msg)
//        }
//        mHandler.postDelayed(mRunnable, 1000);
//        mToast!!.show()
//    }

    fun show(context: Context, msg: CharSequence) {
        if (mToast != null) {
            mToast!!.cancel()
            mToast = null
        }
        mToast = Toast.makeText(context, msg, Toast.LENGTH_SHORT)
        mToast!!.setText(msg)
        mToast!!.show()
    }

    fun showNetUnAvailableToast() {
        show("网络不可用")
    }
}